package com.bdth.controller;

import com.bdth.model.ETreeNode;
import com.bdth.model.EnclosureConfig;
import com.bdth.model.User;
import com.bdth.service.IEnclosureService;
import com.bdth.utils.HtmlUtil;
import com.bdth.utils.JacksonUtils;
import com.bdth.utils.SessionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author: 徐斌
 * @Date: 2018-04-13 10:42
 */
@Controller
@RequestMapping("/map")
public class MapController extends BaseController {

	@Autowired
	private IEnclosureService enclosureService;

	@RequestMapping("/hisroute")
	public ModelAndView hisroute(int dvcId, String dvcSim,
	                             HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		User user = SessionUtils.getUser(request);
		Map<String, Object> context = getRootMap();
		if (user == null) {
			String path = request.getContextPath();
			response.sendRedirect(path + "/login");
		}
		context.put("dvcId", dvcId);
		context.put("dvcSim", dvcSim);
		context.put("userInfo", JacksonUtils.obj2json(user));
		return forword("/map/hisroute", context);
	}


	/**
	 * 生成区域对象树,用于js显示树
	 */
	@RequestMapping("/tree")
	public void tree(HttpServletRequest request, HttpServletResponse response) throws Exception {
		Map<String, Object> map = new HashMap<String, Object>();

		User user = SessionUtils.getUser(request);
		if (user == null) {
			String path = request.getContextPath();
			response.sendRedirect(path + "/login");
		}
		List<Integer> depIds = getDepIds(user.getDepId());
		map.put("depIds", depIds);
		map.put("userName", user.getLoginName());
		List<EnclosureConfig> ls = enclosureService.queryByMapList(map);
		List<ETreeNode> ETreeNodes = new ArrayList<ETreeNode>();
		Map<Integer, ETreeNode> nodeMap = new HashMap<Integer, ETreeNode>();
		ETreeNode polygonNode = new ETreeNode("polygon", "多边形电子围栏", "", "");
		polygonNode.state = "open";
		ETreeNode rectNode = new ETreeNode("rect", "矩形电子围栏", "", "");
		rectNode.state = "open";
		ETreeNode circleNode = new ETreeNode("circle", "圆形电子围栏", "", "");
		circleNode.state = "open";
		ETreeNode keyPointNode = new ETreeNode("keyPoint", "关键点", "", "");
		keyPointNode.state = "open";
		ETreeNode routeNode = new ETreeNode("route", "路线", "", "");
		routeNode.state = "open";
		ETreeNode markerNode = new ETreeNode("marker", "地图标注", "", "");
		markerNode.state = "open";
		for (EnclosureConfig ec : ls) {
			ETreeNode tn = null;
			int keyPoint = 0;
			if (EnclosureConfig.POLYGON.equals(ec.getEnclosureType())) {
				tn = new ETreeNode("" + ec.getId(), ec.getName(), "polygon");
				tn.children = null;
				tn.leaf = true;
				polygonNode.children.add(tn);
			} else if (EnclosureConfig.RECT.equals(ec.getEnclosureType())) {
				tn = new ETreeNode("" + ec.getId(), ec.getName(), "rect");
				tn.children = null;
				tn.leaf = true;
				rectNode.children.add(tn);
			} else if (EnclosureConfig.CIRCLE.equals(ec.getEnclosureType())) {
				tn = new ETreeNode("" + ec.getId(), ec.getName(), "circle");
				tn.children = null;
				circleNode.children.add(tn);
			} else if (EnclosureConfig.MARKER.equals(ec.getEnclosureType())) {
				tn = new ETreeNode("" + ec.getId(), ec.getName(), "marker");
				tn.children = null;
				tn.leaf = true;
				markerNode.children.add(tn);
			}

			if (tn != null) {
				tn.attributes.put("lat", ec.getCenterLat());
				tn.attributes.put("lng", ec.getCenterLng());
				tn.children = null;
				tn.state = "open";
				tn.leaf = true;
			}
		}

		if (polygonNode.children.size() > 0)
			ETreeNodes.add(polygonNode);
		if (rectNode.children.size() > 0)
			ETreeNodes.add(rectNode);
		if (circleNode.children.size() > 0)
			ETreeNodes.add(circleNode);
		if (keyPointNode.children.size() > 0)
			ETreeNodes.add(keyPointNode);
		if (routeNode.children.size() > 0)
			ETreeNodes.add(routeNode);
		if (markerNode.children.size() > 0)
			ETreeNodes.add(markerNode);

		HtmlUtil.writerJson(response, ETreeNodes);
	}

}
